﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ProGaiSuanRule
    {
        //#region 导出
        ///// <summary>
        ///// 用户列表（导出Excel）
        ///// </summary>
        ///// <returns></returns>
        //public static async Task<MemoryStream> GetExportList(List<string> ids, List<ColumnModel> cols)
        //{
        //    //List<string> ids = new List<string>();
        //    //List<ColumnModel> cols = new List<ColumnModel>();

        //    //取出数据源
        //    DataTable exportTable = await GetExportList(ids);
        //    //设置导出格式
        //    ExcelConfig excelconfig = new ExcelConfig();
        //    excelconfig.Title = "概算列表";
        //    excelconfig.TitleFont = "微软雅黑";
        //    excelconfig.TitlePoint = 25;
        //    //excelconfig.HeadPoint = 12;
        //    excelconfig.FileName = "概算列表.xls";
        //    excelconfig.IsAllSizeColumn = true;
        //    //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
        //    excelconfig.ColumnEntity = new List<ColumnModel>();
        //    for (int i = 0; i < cols.Count; i++)
        //    {
        //        excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
        //    }

        //    return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        //}

        //public async static Task<DataTable> GetExportList(List<string> ids)
        //{
        //    //throw new NotImplementedException();
        //    string yids = string.Join("','", ids);
        //    yids = "'" + yids + "'";
        //    //View_ProGaiSpcLists
        //    //string strsql = "select * from View_ProGaiSpcLists where id in (" + yids + ")";
        //    string strsql = "select * from View_ProGaiSpcLists where id in (" + yids + ")";
        //    return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        //}

        //#endregion

        //查询列表
        public static async Task<(DataTable, int)> GetGSIndexList(ProGaiSuanQuerry querry, string strdepids)//string uid
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jsdw)))
            {
                str = str + " and BuildUnit in ('" + querry.Jsdw + "')";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmfl)))
            {
                str = str + " and xmflname = '" + querry.Xmfl + "'";
            }
            if (!(string.IsNullOrEmpty(querry.Begindate.ToString())))
            {
                str = str + " and Begindate >= ''" + querry.Begindate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
            {
                str = str + " and Begindate <= ''" + querry.Enddate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and status = '" + querry.Status + "'";
                //if (querry.Status == "1")
                //{
                //    str = str + " and xmid in (select distinct xmid from View_ProLXSPZT where nextspr = ''" + uid + "'')";
                //}
                //if (querry.Status == "2")
                //{
                //    str = str + " and xmid in (select distinct xmid from View_ProLXSPZT where status = ''2'')";
                //}
                //if (querry.Status == "3")
                //{
                //    str = str + " and xmid in (select distinct xmid from View_ProLXSPZT where status = ''1'')";
                //}
                //if (querry.Status == "4")
                //{
                //    str = str + " and xmid in (select distinct xmid from View_ProLXSPZT where status = ''0'')";
                //}
                //if (querry.Status == "100")
                //{
                //    str = str + " and xmid not in (select htid xmid from dbo.CProGD) ";
                //}
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProLXLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "xmid";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.column, isAsc, querry.size, querry.current);

        }


        //查询列表
        public static async Task<(DataTable, int)> GetGSIndexListN(ProGaiSuanQuerryN querry, string strdepids)//string uid
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.Jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.xmbm)))
            {
                str = str + " and xmbm like '%" + querry.xmbm + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.xmnf)))
            {
                str = str + " and xmnf = '" + querry.xmnf + "'";
            }

            if (!(string.IsNullOrEmpty(querry.Xmfl)))
            {
                str = str + " and xmflname = '" + querry.Xmfl + "'";
            }

            if (!(string.IsNullOrEmpty(querry.gsmc)))
            {
                str = str + " and dxmc like '%" + querry.gsmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and spstatus = '" + querry.Status + "'";
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProGaiSLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "gsbm";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //查询列表
        public static async Task<(IEnumerable<dynamic>, int)> GetGSPCIndexList(ProGaiSuanPCQuerry querry, string strdepids)//string uid
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.Jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmnd)))
            {
                str = str + " and xmnf = '" + querry.Xmnd + "'";
            }
            if (!(string.IsNullOrEmpty(querry.Begindate.ToString())))
            {
                str = str + " and Begindate >= ''" + querry.Begindate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
            {
                str = str + " and Begindate <= ''" + querry.Enddate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and spstatus = '" + querry.Status + "'";
            }
            //添加项目id
            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmid = '" + querry.xmid + "'";
            }
            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProGaiSpcLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                //根据提交时间
                querry.column = "tjsj";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public static async Task<CProManageGSbgS> FindEntityGSBG(string keyValue)
        {
            string sql = "select * from CProManageGSbgS where id = '" + keyValue + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity<CProManageGSbgS>(sql, null);
        }

        //根据id查询单条概算数据
        public static async Task<dynamic> GetGSById(string keyValue)
        {
            string sql = "select * from View_ProGaiSLists where id = '" + keyValue + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        //根据id查询单条概算批次数据
        public static async Task<dynamic> GetGSpcById(string keyValue)
        {
            string sql = "select * from View_ProGaiSpcLists where id = '" + keyValue + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        //固定单体/概算列表
        public static async Task<DataTable> GetGSListByXmid(string xmid)
        {
            //string sql = "select * from View_ProGaiSLists where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
            //return await new RepositoryFactory().BaseRepository().FindList(sql, null);


            DataTable dts = new DataTable();
            dts.Columns.Add("id");
            dts.Columns.Add("bh");//新增
            dts.Columns.Add("pid");
            dts.Columns.Add("dxmc");
            dts.Columns.Add("units");
            dts.Columns.Add("nums");
            dts.Columns.Add("prices");
            dts.Columns.Add("gspsje");
            dts.Columns.Add("bgje");
            dts.Columns.Add("gszje");
            dts.Columns.Add("remark");//新增
            dts.Columns.Add("spstatus");

            dts.Columns.Add("lev");
            List<dynamic> lists = new List<dynamic>();

            string sql = @"select id,xmid,pid,dxmc, gspsje,
ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)bgje,
gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0) gszje
,units,nums,prices from
CProManageGS where pid ='0' and xmid = '" + xmid + "' order by id";

            sql = "select * from View_ProGaiSLists where pid ='0' and xmid = '" + xmid + "' order by id";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);



            //var table = await new RepositoryFactory().BaseRepository().FindList(sql, null);
            for (int num = 0; num < table.Rows.Count; num++)
            {
                //dynamic d = new ExpandoObject();
                DataRow dr = dts.NewRow();
                dr["id"] = table.Rows[num]["id"].ToString();
                dr["bh"] = (num + 1).ToString();
                dr["pid"] = table.Rows[num]["pid"].ToString();
                dr["dxmc"] = table.Rows[num]["dxmc"].ToString();

                dr["gspsje"] = table.Rows[num]["gspsje"].ToDecimal();
                dr["bgje"] = table.Rows[num]["bgje"].ToDecimal();
                dr["gszje"] = table.Rows[num]["gszje"].ToDecimal();

                dr["units"] = table.Rows[num]["units"].ToString();
                dr["nums"] = table.Rows[num]["nums"].ToInt();
                dr["prices"] = table.Rows[num]["prices"].ToDecimal();

                dr["remark"] = table.Rows[num]["remark"].ToString();
                dr["spstatus"] = table.Rows[num]["spstatus"].ToString();

                dr["lev"] = "1";

                dts.Rows.Add(dr);

                string pid = table.Rows[num]["id"].ToString();
                string sql2 = @"select id,xmid,pid,dxmc, gspsje,
            ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)bgje,
            gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0) gszje
            ,units,nums,prices from CProManageGS where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";

                sql2 = "select * from View_ProGaiSLists where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
                DataTable table2 = await new RepositoryFactory().BaseRepository().FindTable(sql2, null);
                List<dynamic> ll = new List<dynamic>();
                for (int i = 0; i < table2.Rows.Count; i++)
                {

                    DataRow dr2 = dts.NewRow();
                    dr2["id"] = table2.Rows[i]["id"].ToString();
                    dr2["bh"] = dr["bh"].ToString() + "." + (i + 1).ToString();

                    dr2["pid"] = table2.Rows[i]["pid"].ToString();
                    dr2["dxmc"] = table2.Rows[i]["dxmc"].ToString();

                    dr2["gspsje"] = table2.Rows[i]["gspsje"].ToDecimal();
                    dr2["bgje"] = table2.Rows[i]["bgje"].ToDecimal();
                    dr2["gszje"] = table2.Rows[i]["gszje"].ToDecimal();

                    dr2["units"] = table2.Rows[i]["units"].ToString();
                    dr2["nums"] = table2.Rows[i]["nums"].ToInt();
                    dr2["prices"] = table2.Rows[i]["prices"].ToDecimal();

                    dr2["remark"] = table2.Rows[i]["remark"].ToString();
                    dr2["spstatus"] = table2.Rows[i]["spstatus"].ToString();

                    dr2["lev"] = "2";

                    dts.Rows.Add(dr2);
                }


            }
            return dts;

        }

        public static async Task<IEnumerable<dynamic>> GetDXListByXMid(string xmid)
        {
            string strsql = @"select id,dxmc,dxbm,lxje,(select dxmc from CProManageLX where id = tlx.pid)dxlx,pid,remark,
(select dxmc+',' from CProManageGS where id in (select hbid from CProManageLXHB where dxid = tlx.id)
 for XML path('')
)gsmc,status,(case 
when ISNULL(status,-1) = '-1' then 'draft' 
when ISNULL(status,-1) = '0' then 'uncheck' 
when ISNULL(status,-1) = '1' then 'checking' 
when ISNULL(status,-1) = '2' then 'checked'
when ISNULL(status,-1)  = '3' then 'reject'
when ISNULL(status,-1)  = '4' then 'invalid'
when ISNULL(status,-1)  = '5' then 'cancel' end
)spstatus

 from CProManageLX tlx
where xmid = '" + xmid + "' and pid <> '0' order by dxlx asc,id asc";
            return await new RepositoryFactory().BaseRepository().FindList(strsql, null);
        }

        public static async Task<IEnumerable<dynamic>> GetGSDXListByGsid(string gsid)
        {
            //string sql = "select * from View_ProGaiSLists where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
            string strsql = @"select hbid gsid,CProManageGS.dxmc gsmc,
CProManageGS.gspsje,CProManageGS.gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)gszje,
dxid,CProManageLX.dxmc dxmc, CProManageLX.lxje
  from CProManageLXHB
 left join CProManageGS on hbid = CProManageGS.id
left join CProManageLX on dxid = CProManageLX.id
where hbid = '" + gsid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(strsql, null);
        }

        public static async Task<IEnumerable<dynamic>> GetGSDXList(string xmid)
        {
            //string sql = "select * from View_ProGaiSLists where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
            string strsql = @"select hbid gsid,CProManageGS.dxmc gsmc,
CProManageGS.gspsje,CProManageGS.gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)gszje,
dxid,tlx.dxmc dxmc, tlx.lxje,tlx.dxbm,tlx.remark,(select dxmc from CProManageLX where id = tlx.pid)dxlx
  from CProManageLXHB
 left join CProManageGS on hbid = CProManageGS.id
left join CProManageLX tlx on dxid = tlx.id
where hbid in (select id from CProManageGS where xmid = '" + xmid + "')";
            return await new RepositoryFactory().BaseRepository().FindList(strsql, null);
        }

        //固定单体/概算列表-树状
        public static async Task<List<dynamic>> GetGSTreeByXmid(string xmid, string lx = "all")
        {
            List<dynamic> lists = new List<dynamic>();

            string sql = @"";

            if (lx == "all")
            {
                sql = "select * from View_ProGaiSLists where pid ='0' and xmid = '" + xmid + "' order by id";
            }
            else
            {
                sql = @"select * from View_ProGaiSLists 
where id in (

select distinct pid from CProManageGS where pcid in (
select id from CProManageGSpc where xmid = '" + xmid + @"' and status = '2'
)

) and pid ='0' and xmid = '" + xmid + "' order by id";
            }

            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);



            //var table = await new RepositoryFactory().BaseRepository().FindList(sql, null);
            for (int num = 0; num < table.Rows.Count; num++)
            {
                dynamic d = new ExpandoObject();
                d.id = table.Rows[num]["id"].ToString();
                d.pid = table.Rows[num]["pid"].ToString();

                d.gsbm = table.Rows[num]["gsbm"].ToString();
                d.dxmc = table.Rows[num]["dxmc"].ToString();

                d.gspsje = table.Rows[num]["gspsje"].ToDecimal();
                d.bgje = table.Rows[num]["bgje"].ToDecimal();
                d.gszje = table.Rows[num]["gszje"].ToDecimal();

                d.units = table.Rows[num]["units"].ToString();
                d.nums = table.Rows[num]["nums"].ToInt();
                d.prices = table.Rows[num]["prices"].ToDecimal();

                d.remark = table.Rows[num]["remark"].ToString();
                d.gl = table.Rows[num]["gl"].ToString();
                //是否可选择
                d.kxz = table.Rows[num]["kxz"].ToString();
                //py bh cf
                d.zt = table.Rows[num]["zt"].ToString();
                d.gsye = table.Rows[num]["gsye"].ToString();

                d.spstatus = table.Rows[num]["spstatus"].ToString();

                d.lev = "1";


                string pid = table.Rows[num]["id"].ToString();
                string sql2 = @"";

                if (lx == "all")
                {
                    sql2 = "select * from View_ProGaiSLists where pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
                }
                else
                {
                    sql2 = @"select * from View_ProGaiSLists 
where pcid in (
select id from CProManageGSpc where xmid = '" + xmid + @"' and status = '2'
) and pid ='" + pid + "' and xmid = '" + xmid + "' order by id";
                }
                DataTable table2 = await new RepositoryFactory().BaseRepository().FindTable(sql2, null);
                List<dynamic> ll = new List<dynamic>();
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    dynamic dd = new ExpandoObject();
                    dd.id = table2.Rows[i]["id"].ToString();
                    dd.pid = table2.Rows[i]["pid"].ToString();
                    dd.gsbm = table2.Rows[i]["gsbm"].ToString();

                    dd.dxmc = table2.Rows[i]["dxmc"].ToString();

                    dd.gspsje = table2.Rows[i]["gspsje"].ToDecimal();
                    dd.bgje = table2.Rows[i]["bgje"].ToDecimal();
                    dd.gszje = table2.Rows[i]["gszje"].ToDecimal();

                    dd.units = table2.Rows[i]["units"].ToString();
                    dd.nums = table2.Rows[i]["nums"].ToInt();
                    dd.prices = table2.Rows[i]["prices"].ToDecimal();

                    dd.remark = table2.Rows[i]["remark"].ToString();

                    dd.gl = table2.Rows[i]["gl"].ToString();
                    //是否可选择
                    dd.kxz = table2.Rows[i]["kxz"].ToString();
                    //py bh cf
                    dd.zt = table2.Rows[i]["zt"].ToString();

                    dd.gsye = table2.Rows[i]["gsye"].ToString();

                    dd.spstatus = table2.Rows[i]["spstatus"].ToString();

                    dd.lev = "2";

                    ll.Add(dd);
                }

                d.childs = ll;

                lists.Add(d);
            }
            return lists;
        }

        //固定单体/概算列表-树状
        public static async Task<List<dynamic>> GetGSTreeByPCid(string pcid)
        {
            //DataTable dts = new DataTable();
            //dts.Columns.Add("id");
            //dts.Columns.Add("pid");
            //dts.Columns.Add("dxmc");
            //dts.Columns.Add("units");
            //dts.Columns.Add("nums");
            //dts.Columns.Add("prices");
            //dts.Columns.Add("gspsje");
            //dts.Columns.Add("bgje");
            //dts.Columns.Add("gszje");

            //dts.Columns.Add("spstatus");

            //dts.Columns.Add("lev");
            List<dynamic> lists = new List<dynamic>();

            string sql = @"select id,xmid,pid,dxmc, gspsje,
ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)bgje,
gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0) gszje
,units,nums,prices from
CProManageGS where pid ='0' and id = '" + pcid + "' order by id";


            sql = @"select * from View_ProGaiSLists where pid ='0' and id in
(select pid from CProManageGS where pcid = '" + pcid + "' ) order by id";

            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);



            //var table = await new RepositoryFactory().BaseRepository().FindList(sql, null);
            for (int num = 0; num < table.Rows.Count; num++)
            {
                dynamic d = new ExpandoObject();
                d.id = table.Rows[num]["id"].ToString();
                d.pid = table.Rows[num]["pid"].ToString();
                d.dxmc = table.Rows[num]["dxmc"].ToString();
                d.gsbm = table.Rows[num]["gsbm"].ToString();

                d.gspsje = table.Rows[num]["gspsje"].ToDecimal();
                d.bgje = table.Rows[num]["bgje"].ToDecimal();
                d.gszje = table.Rows[num]["gszje"].ToDecimal();

                d.units = table.Rows[num]["units"].ToString();
                d.nums = table.Rows[num]["nums"].ToInt();
                d.prices = table.Rows[num]["prices"].ToDecimal();

                d.remark = table.Rows[num]["remark"].ToString();
                d.gl = table.Rows[num]["gl"].ToString();

                d.spstatus = table.Rows[num]["spstatus"].ToString();

                d.lev = "1";


                string pid = table.Rows[num]["id"].ToString();
                string sql2 = @"select id,xmid,pid,dxmc, gspsje,
            ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0)bgje,
            gspsje + ISNULL((select SUM(bgsdje) from CProManageGSbgS where gsid = CProManageGS.id and status = '2'),0) gszje
            ,units,nums,prices from CProManageGS where pid ='" + pid + "' and pcid = '" + pcid + "' order by id";

                sql2 = "select * from View_ProGaiSLists where pid ='" + pid + "' and pcid = '" + pcid + "' order by id";
                DataTable table2 = await new RepositoryFactory().BaseRepository().FindTable(sql2, null);
                List<dynamic> ll = new List<dynamic>();
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    dynamic dd = new ExpandoObject();
                    dd.id = table2.Rows[i]["id"].ToString();
                    dd.pid = table2.Rows[i]["pid"].ToString();
                    dd.dxmc = table2.Rows[i]["dxmc"].ToString();
                    dd.gsbm = table2.Rows[i]["gsbm"].ToString();

                    dd.gspsje = table2.Rows[i]["gspsje"].ToDecimal();
                    dd.bgje = table2.Rows[i]["bgje"].ToDecimal();
                    dd.gszje = table2.Rows[i]["gszje"].ToDecimal();

                    dd.units = table2.Rows[i]["units"].ToString();
                    dd.nums = table2.Rows[i]["nums"].ToInt();
                    dd.prices = table2.Rows[i]["prices"].ToDecimal();

                    dd.remark = table2.Rows[i]["remark"].ToString();
                    dd.gl = table2.Rows[i]["gl"].ToString();


                    dd.spstatus = table2.Rows[i]["spstatus"].ToString();


                    dd.lev = "2";

                    ll.Add(dd);
                }

                d.childs = ll;

                lists.Add(d);
            }
            return lists;
        }

        //单项个数
        public static async Task<IEnumerable<dynamic>> GetLXCountById(string xmid)
        {
            string sql = "select count(*)cc from (select *,(select count(*) from  CProManageLX where pid = a.id )cc from dbo.CProManageLX a)t where cc = 0 and xmid = '" + xmid + "' ";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //概算个数
        public static async Task<IEnumerable<dynamic>> GetGSCountById(string xmid)
        {
            string sql = "select count(*)cc from (select *,(select count(*) from  CProManageGS where pid = a.id )cc from CProManageGS a)t where cc = 0 and xmid = '" + xmid + "' ";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }


        //List<CProManageGSPC> progse = null;
        //CProManageGSpc pc = new CProManageGSpc();

        //新增修改
        public static async Task InsertCProManageGS(CProManageGSpc pc, List<CProManageGS> progse, string ope, string uid)
        {
            if (ope == "add")
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    await irp.Insert(pc);

                    for (int i = 0; i < progse.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(progse[i].id))
                        {
                            CProManageGS jhe = await irp.FindEntity<CProManageGS>(new { id = progse[i].id, xmid = pc.xmid });
                            if (null != jhe)
                            {

                            }
                            else
                            {
                                progse[i].id = CommonHelper.GetUniqueId().ToString();
                                progse[i].xmid = pc.xmid;
                                progse[i].pcid = "";//类型不给批次id pc.id
                                progse[i].pid = "0";
                                await irp.Insert(progse[i]);
                            }
                        }
                        else
                        {
                            progse[i].id = CommonHelper.GetUniqueId().ToString();
                            progse[i].xmid = pc.xmid;
                            progse[i].pcid = "";//类型不给批次id pc.id
                            progse[i].pid = "0";
                            await irp.Insert(progse[i]);
                        }
                        await GetInsert(irp, pc.xmid, pc.id, progse[i].id, progse[i].childs);
                    }

                    return true;

                });
            }
            else
            {
                await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    pc.createdate = DateTime.Now;
                    await irp.Update(pc);

                    if (null != progse && progse.Count > 0)
                    {
                        string strsql = @"delete from CProManageGS where id in (
select id from (
select *,(select COUNT(*) from CProManageGS where pid = tgs.id)cc,
(select COUNT(*) from CProManageGS where pid = tgs.id and pcid = '" + pc.id + @"')ccc
 from CProManageGS tgs 
where xmid = (select xmid from  CProManageGSpc where id = '" + pc.id + @"') and pid = '0')t where cc = ccc and cc<> 0
)";
                        await irp.ExecuteSql(strsql);

                        strsql = "delete from CProManageGS where pcid = '" + pc.id + "'";
                        await irp.ExecuteSql(strsql);

                        for (int i = 0; i < progse.Count; i++)
                        {

                            if (!string.IsNullOrEmpty(progse[i].id))
                            {
                                CProManageGS jhe = await irp.FindEntityByKey<CProManageGS>(progse[i].id);
                                if (null != jhe)
                                {

                                }
                                else
                                {
                                    progse[i].id = CommonHelper.GetUniqueId().ToString();
                                    progse[i].xmid = pc.xmid;
                                    progse[i].pcid = "";//类型不给批次id pc.id
                                    progse[i].pid = "0";
                                    await irp.Insert(progse[i]);
                                }
                            }
                            else
                            {
                                progse[i].id = CommonHelper.GetUniqueId().ToString();
                                progse[i].xmid = pc.xmid;
                                progse[i].pcid = "";//类型不给批次id pc.id
                                progse[i].pid = "0";
                                await irp.Insert(progse[i]);
                            }

                            //progse[i].id = CommonHelper.GetUniqueId().ToString();
                            //progse[i].xmid = pc.xmid;
                            //progse[i].pcid = pc.id;
                            //progse[i].pid = "0";
                            //await irp.Insert(progse[i]);

                            await GetInsert(irp, pc.xmid, pc.id, progse[i].id, progse[i].childs);
                        }
                    }

                    return true;

                });
            }

            //CProManageGS count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGS>(pc.id);
            //if (count is null)
            //{
            //    string strid = CommonHelper.GetUniqueId().ToString();
            //    progs.id = strid;
            //    progs.createuser = uid;
            //    if (string.IsNullOrEmpty(progs.status))
            //    {
            //        progs.status = "2";
            //    }
            //    progs.remark = "1";
            //    if (string.IsNullOrEmpty(progs.pid))
            //    {
            //        throw new Exception("pid不能为空");
            //    }
            //    if (progs.pid == "0")
            //    {
            //        throw new Exception("只能添加二级节点");
            //    }
            //    await new RepositoryFactory().BaseRepository().Insert(progs);
            //}
            //else
            //{
            //    await new RepositoryFactory().BaseRepository().Update(progs);
            //}
        }

        public async static Task GetInsert(IRepository irp, string xmid, string pcid, string pid, List<CProManageGS> progse)
        {
            if (null != progse)
            {
                for (int i = 0; i < progse.Count; i++)
                {
                    progse[i].id = CommonHelper.GetUniqueId().ToString();
                    progse[i].xmid = xmid;
                    progse[i].pcid = pcid;
                    progse[i].pid = pid;

                    await irp.Insert(progse[i]);

                    await GetInsert(irp, xmid, pcid, progse[i].id, progse[i].childs);
                }
            }
        }

        public static async Task InsertCProManageGS(CProManageGS progs, string ope, string uid)
        {
            if (ope.ToLower() == "add")
            {


                //查询父节点的pid ，如果是0 那么就是2级菜单
                CProManageGS pgs = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGS>(progs.pid);

                if (null == pgs || pgs.pid != "0")
                {
                    throw new Exception("只能添加二级节点");
                }
                await new RepositoryFactory().BaseRepository().Insert(progs);
            }
            else
            {
                await new RepositoryFactory().BaseRepository().Update(progs);
            }
        }

        //删除
        public static async Task DeleteCProManageGS(string dxid)
        {
            CProManageGS count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGS>(dxid);
            if (count is null)
            {
                throw new Exception("没有数据");
            }
            if (count.pid == "0")
            {
                throw new Exception("不可删除父节点");
            }

            string sql = "select ISNULL(sqje,0.00)sqje from View_ProFKSQLists where dxid = (SELECT dxid from CProManageLXHB where HBid = '" + dxid + "')";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            string str2 = "0";
            for (int i = 0; i < table.Rows.Count; i++)
            {
                str2 = table.Rows[i]["sqje"].ToString();
            }
            if ((str2 == "0.00") || (str2 == "0"))
            {
                string delsql = "delete from dbo.CProConBG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProConBZJ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProConFKJH where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProFKMX where sqid in (select id from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "')))\r\n\r\ndelete from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProJS where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProJG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "'))\r\n\r\ndelete from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "')\r\n\r\ndelete from dbo.CProZB where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + dxid + "')\r\n\r\ndelete from CProManageLX where id = (SELECT dxid from CProManageLXHB where HBid = '" + dxid + "')\r\n\r\ndelete from CProManageLXHB where HBid = '" + dxid + "'\r\n\r\ndelete from CProManageGS where id = '" + dxid + "'";

                await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            }
            else
            {
                throw new Exception("该标段已有付款，不能删除");
            }

        }

        //删除多条
        public static async Task DeleteCProManageGSMulti(Root ids)
        {

            foreach (BaseDeleteMulti idm in ids.ids)
            {
                CProManageGS count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGS>(idm.id);
                if (count is null)
                {
                    throw new Exception("没有数据");
                }
                //if (count.pid == "0")
                //{
                //    throw new Exception("不可删除父节点");
                //}

                string sql = "select ISNULL(sqje,0.00)sqje from View_ProFKSQLists where dxid = (SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')";
                DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                string str2 = "0";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    str2 = table.Rows[i]["sqje"].ToString();
                }
                if ((str2 != "0.00") && (str2 != "0"))
                {
                    throw new Exception("对应标段已有付款，不能删除");
                }
            }
            foreach (BaseDeleteMulti idm in ids.ids)
            {
                string delsql = "delete from dbo.CProConBG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProConBZJ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProConFKJH where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProFKMX where sqid in (select id from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')))\r\n\r\ndelete from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProJS where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProJG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from dbo.CProZB where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from CProManageLX where id = (SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from CProManageLXHB where HBid = '" + idm.id + "'\r\n\r\ndelete from CProManageGS where id = '" + idm.id + "'";

                await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            }

        }

        //删除多条
        public static async Task DeleteCProManageGSPCMulti(Root ids)
        {

            foreach (BaseDeleteMulti idm in ids.ids)
            {
                if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.概算))
                {
                    throw new Exception("请确认数据权限");
                }
            }


            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    await iRep.DeleteAny<CProManageGS>(new { pcid = idm.id });
                    await iRep.DeleteAny<CProManageGSpc>(new { id = idm.id });
                    //await iRep.Delete<CProManageGSpc>(new { id = idm.id });
                    //await new RepositoryFactory().BaseRepository().ExecuteProc("deleteproject", new { proid = idm.id });
                }
                return true;
            });

            //foreach (BaseDeleteMulti idm in ids.ids)
            //{
            //    CProManageGSpc count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGSpc>(idm.id);
            //    if (count is null)
            //    {
            //        throw new Exception("没有数据");
            //    }
            //    //if (count.pid == "0")
            //    //{
            //    //    throw new Exception("不可删除父节点");
            //    //}

            //    string sql = "select ISNULL(sqje,0.00)sqje from View_ProFKSQLists where dxid = (SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')";
            //    DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            //    string str2 = "0";
            //    for (int i = 0; i < table.Rows.Count; i++)
            //    {
            //        str2 = table.Rows[i]["sqje"].ToString();
            //    }
            //    if ((str2 != "0.00") && (str2 != "0"))
            //    {
            //        throw new Exception("对应标段已有付款，不能删除");
            //    }
            //}
            //foreach (BaseDeleteMulti idm in ids.ids)
            //{
            //    string delsql = "delete from dbo.CProConBG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProConBZJ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProConFKJH where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProFKMX where sqid in (select id from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')))\r\n\r\ndelete from dbo.CProFKSQ where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProJS where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProJG where htid in (select htid from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "'))\r\n\r\ndelete from dbo.CProContract where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from dbo.CProZB where dxid =(SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from CProManageLX where id = (SELECT dxid from CProManageLXHB where HBid = '" + idm.id + "')\r\n\r\ndelete from CProManageLXHB where HBid = '" + idm.id + "'\r\n\r\ndelete from CProManageGS where id = '" + idm.id + "'";

            //    await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            //}
        }

        #region 概算变更


        //查询列表
        public static async Task<(IEnumerable<dynamic>, int)> GetGSBGIndexList(dynamic querry, string strdepids)//string uid
        {

            string size = querry.size;
            string current = querry.current;

            string sort = querry.sort?? "";
            string column = querry.column?? "";

            string Xmmc = querry.Xmmc;
            string Status = querry.Status;
            string bgxz = querry.bgxz;

            string bgmc = querry.bgmc;
            string dxmc = querry.dxmc;
            

            string str = "";
 
            if (!(string.IsNullOrEmpty(Xmmc)))
            {
                str = str + " and xmmc like '%" + Xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(bgxz)))
            {
                str = str + " and bgxz = '" + bgxz + "'";
            }
            if (!(string.IsNullOrEmpty(bgmc)))
            {
                str = str + " and bgmc like '%" + bgmc + "%'";
            }
            if (!(string.IsNullOrEmpty(dxmc)))
            {
                str = str + " and dxmc like '%" + dxmc + "%'";
            }




            if (!(string.IsNullOrEmpty(Status)))
            {
                str = str + " and spstatus = '" + Status + "'";

            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProGaiSBGLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            
            bool isAsc = false;
            if (sort == "Asc" || sort == "asc")
            {
                isAsc = true;
            }

            if (string.IsNullOrEmpty(column))
            {
                column = "xmid";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, column, isAsc, size.ToInt(), current.ToInt());

        }

        public async static Task<DataTable> GetGSBGList(string strgsid)
        {
            string strsql = "select * from View_ProGaiSBGLists where gsid = '" + strgsid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }


        public async static Task<IEnumerable<dynamic>> GetGSBGListByxmid(string strxmid)
        {
            string strsql = "select * from View_ProGaiSBGLists where xmid = '" + strxmid + "'";

            return await new RepositoryFactory().BaseRepository().FindList(strsql);
        }

        public async static Task<dynamic> GetGSBGByid(string bgid)
        {
            string strsql = "select * from View_ProGaiSBGLists where id = '" + bgid + "'";

            return await new RepositoryFactory().BaseRepository().FindEntity(strsql);

        }

        //概算比例
        public async static Task<dynamic> GetGSBGScale(CProManageGSbgS gsbg)
        {
            string strsql = @"
select *,CONVERT(decimal(18,2),bgje*100.00 /gspsje)bl from (
select gspsje,ISNULL(
(select SUM(ISNULL(bgsdje, 0)) from CProManageGSbgS where gsid = CProManageGS.id
and id <> '" + gsbg.id + @"' and status in ('1', '2')
),0)+ISNULL("+ gsbg.sqje + ", 0) bgje from CProManageGS where id = '"+ gsbg.gsid + "')t ";
            return await new RepositoryFactory().BaseRepository().FindEntity(strsql);
        }


        public async static Task<int> InsertGSBG(CProManageGSbgS gsbg, string ope)
        {
            if (ope == "add")
            {
                return await new RepositoryFactory().BaseRepository().Insert(gsbg);
            }
            else// (ope == "edit")
            {
                return await new RepositoryFactory().BaseRepository().Update(gsbg);
            }
        }

        public async static Task<int> DeleteGSBG(string bgid)
        {
            return await new RepositoryFactory().BaseRepository().DeleteAny<CProManageGSbgS>(new { id = bgid });
        }

        #endregion


        #region 暂时不要


        //概算变更
        public static async Task GSChange(string gsid, string xmid)
        {

            string updsql = "update CProManageGS set xmid = '" + xmid + "' where id = '" + gsid + "'";
            await new RepositoryFactory().BaseRepository().ExecuteSql(updsql);

        }
        //概算预测列表
        public static async Task<DataTable> GetGSYCByXmid(string xmid)
        {
            int num;
            DataTable dts = new DataTable();
            dts.Columns.Add("id");
            dts.Columns.Add("dxid");
            dts.Columns.Add("dxmc");
            dts.Columns.Add("xmmc");
            dts.Columns.Add("yczje");
            dts.Columns.Add("ycny");
            dts.Columns.Add("xyyc");
            dts.Columns.Add("bz");

            string sql = "select id,xmid,(select xmmc from CProManage where id = xmid)xmmc,dxmc from CProManageLX where xmid = '" + xmid + "' and pid = '0'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
            for (num = 0; num < table.Rows.Count; num++)
            {
                DataRow row2;
                string maxid;
                DataRow row = dts.NewRow();
                row["id"] = num + 1;
                row["xmmc"] = table.Rows[num]["xmmc"].ToString();
                row["dxmc"] = table.Rows[num]["dxmc"].ToString();

                //row["dxgsje"] = allZD.Rows[num]["dxmc"].ToString();
                dts.Rows.Add(row);
                string sql3 = (@"select id,pid,(select sum(czje) from CProManageGSYC where gsid = CProManageLX.id) yczje,dxmc
,(select czje from CProManageGSYC where gsid =CProManageLX.id and czsj = '" + DateTime.Now.AddMonths(2).AddDays(0 - DateTime.Now.Day).ToString("yyyy-MM-dd") + @"'  )xyyc 
,(select bz from CProManageGSYC where gsid =CProManageLX.id and czsj = '" + DateTime.Now.AddMonths(2).AddDays(0 - DateTime.Now.Day).ToString("yyyy-MM-dd") + @"'  )bz                       
from CProManageLX where pid = '" + table.Rows[num]["id"].ToString() + "'");
                DataTable table3 = await new RepositoryFactory().BaseRepository().FindTable(sql3, null);
                int num2 = 0;
                while (num2 < table3.Rows.Count)
                {
                    row2 = dts.NewRow();
                    maxid = GetMaxid(dts, Convert.ToString((int)(num + 1)));
                    row2["id"] = (num + 1) + "." + maxid;

                    row2["dxid"] = table3.Rows[num2]["id"].ToString();
                    row2["dxmc"] = table3.Rows[num2]["dxmc"].ToString();
                    //精确到万元
                    string zsgsje = Math.Round(Convert.ToDecimal(table3.Rows[num2]["yczje"].ToString()) / 10000, 2).ToString();
                    row2["yczje"] = table3.Rows[num2]["yczje"].ToString();//zsgsje;

                    //给一个预测年月默认值  ==== 下一个月
                    row2["ycny"] = DateTime.Now.AddMonths(2).AddDays(0 - DateTime.Now.Day).ToString("yyyy-MM");
                    row2["xyyc"] = table3.Rows[num2]["xyyc"].ToString();
                    row2["bz"] = table3.Rows[num2]["bz"].ToString();

                    dts.Rows.Add(row2);
                    num2++;
                }

            }
            for (num = 0; num < dts.Rows.Count; num++)
            {
                string source = dts.Rows[num]["id"].ToString();
                if (!source.Contains("."))
                {
                    GetRowSDX(dts, source, dts.Rows[num]);
                }
            }
            DataRow dr = dts.NewRow();
            dr["dxmc"] = "合计";
            GetRowS(dts, dr);
            dts.Rows.InsertAt(dr, 0);

            GetRowNumChinese(dts);

            return dts;
        }
        public static string GetMaxid(DataTable dts, string id)
        {
            string p = "0";
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                if (dts.Rows[i]["id"].ToString().StartsWith(id + "."))
                {
                    string str2 = dts.Rows[i]["id"].ToString();
                    if (Convert.ToInt32(str2.Split(new char[] { '.' })[1]) > Convert.ToInt32(p))
                    {
                        p = str2.Split(new char[] { '.' })[1];
                    }
                }
            }
            return Convert.ToString((int)(Convert.ToInt32(p) + 1));
        }

        public static void GetRowSDX(DataTable dts, string id, DataRow dr)
        {
            decimal num = 0M;
            decimal num2 = 0M;
            decimal num3 = 0M;
            decimal num4 = 0M;
            decimal num5 = 0M;
            decimal num6 = 0M;
            decimal num7 = 0M;
            decimal num9 = 0M;
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                if (dts.Rows[i]["id"].ToString().StartsWith(id + "."))
                {
                    //string str = dts.Rows[i]["dxid"].ToString();
                    num += Convert.ToDecimal(dts.Rows[i]["yczje"].ToString());
                    //num2 += Common.ParseDecimal(dts.Rows[i]["htgsje"].ToString());
                    //num3 += Common.ParseDecimal(dts.Rows[i]["htje"].ToString());
                    //num4 += Common.ParseDecimal(dts.Rows[i]["ecjjje"].ToString());
                    //num5 += Common.ParseDecimal(dts.Rows[i]["bgje"].ToString());
                    //num6 += Common.ParseDecimal(dts.Rows[i]["htzje"].ToString());
                    //num7 += Common.ParseDecimal(dts.Rows[i]["gsye"].ToString());
                    //num9 += Common.ParseDecimal(dts.Rows[i]["wzb"].ToString());
                }
            }
            dr["yczje"] = num.ToString();
            //dr["htgsje"] = num2.ToString();
            //dr["htje"] = num3.ToString();
            //dr["ecjjje"] = num4.ToString();
            //dr["bgje"] = num5.ToString();
            //dr["htzje"] = num6.ToString();
            //dr["gsye"] = num7.ToString();
            //dr["bl"] = (num2 == 0M) ? "0" : Math.Round((decimal) ((num6 * Common.ParseDecimal("100.00")) / num2), 2).ToString();
            //dr["wzb"] = num9.ToString();
        }
        public static void GetRowNumChinese(DataTable dts)
        {
            for (int num = 0; num < dts.Rows.Count; num++)
            {
                string source = dts.Rows[num]["id"].ToString();
                if (!source.Contains(".") && !string.IsNullOrEmpty(source))
                {
                    dts.Rows[num]["id"] = ConvertChinese(source);
                }
            }
        }
        public static string ConvertChinese(string str)
        {
            switch (str)
            {
                case "0":
                    return "";

                case "1":
                    return "一";

                case "2":
                    return "二";

                case "3":
                    return "三";

                case "4":
                    return "四";

                case "5":
                    return "五";

                case "6":
                    return "六";

                case "7":
                    return "七";

                case "8":
                    return "八";

                case "9":
                    return "九";
            }
            return "";
        }
        public static void GetRowS(DataTable dts, DataRow dr)
        {
            decimal num = 0M;
            decimal num2 = 0M;
            decimal num3 = 0M;
            decimal num4 = 0M;
            decimal num5 = 0M;
            decimal num6 = 0M;
            decimal num7 = 0M;
            decimal num9 = 0M;
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                if (!dts.Rows[i]["id"].ToString().Contains("."))
                {
                    num += Convert.ToDecimal(dts.Rows[i]["yczje"].ToString());
                    //num2 += Common.ParseDecimal(dts.Rows[i]["htgsje"].ToString());
                    //num3 += Common.ParseDecimal(dts.Rows[i]["htje"].ToString());
                    //num4 += Common.ParseDecimal(dts.Rows[i]["ecjjje"].ToString());
                    //num5 += Common.ParseDecimal(dts.Rows[i]["bgje"].ToString());
                    //num6 += Common.ParseDecimal(dts.Rows[i]["htzje"].ToString());
                    //num7 += Common.ParseDecimal(dts.Rows[i]["gsye"].ToString());
                    //num9 += Common.ParseDecimal(dts.Rows[i]["wzb"].ToString());
                }
            }
            dr["yczje"] = num.ToString();
            //dr["htgsje"] = num2.ToString();
            //dr["htje"] = num3.ToString();
            //dr["ecjjje"] = num4.ToString();
            //dr["bgje"] = num5.ToString();
            //dr["htzje"] = num6.ToString();
            //dr["gsye"] = num7.ToString();
            //dr["bl"] = (num2 == 0M) ? "0" : Math.Round((decimal) ((num6 * Common.ParseDecimal("100.00")) / num2), 2).ToString();
            //dr["wzb"] = num9.ToString();
        }
        //预测明细
        public static async Task<(DataTable, int)> GetGSYCMXByXmid(CProManageGSYCQuerry querry, string xmid)
        {
            string sql = "select id,(select xmmc from  CProManage where id = xmid)xmmc,gsmc,czsj,czje,tbsj,bz from CProManageGSYC where xmid = '" + xmid + "'";
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "czsj";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.column, isAsc, querry.size, querry.current);

        }
        //根据预测id获取单条预测
        public static async Task<IEnumerable<dynamic>> GetYCByid(string ycid)
        {
            string sql = "select * from CProManageGSYC where id = '" + ycid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //删除多条预测
        public static async Task DeleteCProManageGSYCMulti(Root ids)
        {

            foreach (BaseDeleteMulti idm in ids.ids)
            {
                string delsql = "delete from CProManageGSYC where id = '" + idm.id + "'";

                await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            }


            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    string delsql = "delete from CProFKSQ where id = '" + idm.id + "'";
                    await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
                }
                return true;
            });


        }
        //新增修改预测
        public static async Task InsertCProManageGSYC(CProManageGSYC progsyc, string uid)
        {
            CProManageGSYC count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageGSYC>(progsyc.id);
            if (count is null)
            {
                string strid = CommonHelper.GetUniqueId().ToString();
                progsyc.id = strid;
                progsyc.createuser = uid;
                progsyc.tbsj = DateTime.Now;

                await new RepositoryFactory().BaseRepository().Insert(progsyc);

            }
            else
            {
                await new RepositoryFactory().BaseRepository().Update(progsyc);
            }
        }

        #endregion
    }
}
